// 本模块为首页视图相关逻辑及状态,主要用来做页面传参
import index from '@/api/index.js'
import news from '@/api/news.js'


const state = {
    // 轮播图数据
    indexBanner: [],
    indexCooperations: [],
    indexGuests: [],
    guestsDetails: {},
}

const getters = {
    indexBanner(state) {
        return state.indexBanner
    },
    indexGuests(state) {
        return state.indexGuests
    },
    indexCooperations(state) {
        return state.indexCooperations
    },
    guestsDetails(state) {
        return state.guestsDetails
    },
}

const mutations = {
    setIndexState(state, { key, value }) {
        state[key] = value
    },
}

const actions = {
    getIndexSwiper({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            index.indexSwiper().then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setIndexState', {
                        key: 'indexBanner',
                        value: res.data.data
                    })
                }
                resolve(res)
            }).catch((error) => {
                reject(error)
            });
        })
    },
    getIndexGuests({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            let data = {
                per_page: 10,
                page: 1,
                category_id: 5,
            }
            news.news(data).then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setIndexState', {
                        key: 'indexGuests',
                        value: res.data.data.data
                    })
                }
                resolve(res)
            }).catch((error) => {
                reject(error)
            });
        })
    },
    getGuestsDetails({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            index.guestsDetails(data.id).then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setIndexState', {
                        key: 'guestsDetails',
                        value: res.data.data
                    })
                }
                resolve(res)
            }).catch((error) => {
                reject(error)
            });
        })
    },
    getIndexPartner({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            index.partner().then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setIndexState', {
                        key: 'indexCooperations',
                        value: res.data.data
                    })
                }
                resolve(res)
            }).catch((error) => {
                reject(error)
            });
        })
    },
}
export default {
    state,
    getters,
    actions,
    mutations
}